package com.ruoyi.community.controller.wxApi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.Trip;
import com.ruoyi.community.domain.vo.TripVo;
import com.ruoyi.community.service.ITripService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行程Controller
 *
 * @author wj
 * @date 2022-02-24
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/community/trip")
@Api(tags = {"行程"})
public class WxTripController extends BaseController
{
    @Autowired
    private ITripService tripService;

    /**
     * 查询行程列表
     */
    @ApiOperation(value = "查询行程列表")
    @GetMapping("/list")
    public TableDataInfo list(Trip trip)
    {
        startPage();
//        Trip tripDto =new Trip();
//        tripDto.setId(trip.getId());
//        tripDto.setUserId(trip.getUserId());
        List<TripVo> list = tripService.selectTripperson(trip);
        if (ObjectUtils.isEmpty(list)){
            return getNoDataTable(list);
        }
        return getDataTable(list);
    }

    /**
     * 导出行程列表
     */
    @ApiOperation(value = "导出行程列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Trip trip)
    {
        List<Trip> list = tripService.selectTripList(trip);
        ExcelUtil<Trip> util = new ExcelUtil<Trip>(Trip.class);
        util.exportExcel(response, list, "行程数据");
    }

    /**
     * 获取行程详细信息
     */
    @ApiOperation(value = "获取行程详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo( Long id)
    {
        return AjaxResult.success(tripService.selectTripById(id));
    }

    /**
     * 获取行程详细信息
     */
    @ApiOperation(value = "获取行程详细信息byUserId")
    @GetMapping(value = "/getInfoByUser")
    public AjaxResult getInfoByUser( Trip trip)
    {
        return AjaxResult.success(tripService.selectTripperson(trip));
    }

    /**
     * 新增行程
     */
    @ApiOperation(value = "新增行程")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Trip trip)
    {
        return toAjax(tripService.insertTrip(trip));
    }

    /**
     * 修改行程
     */
    @ApiOperation(value = "修改行程")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Trip trip)
    {
        return toAjax(tripService.updateTrip(trip));
    }

    /**
     * 删除行程
     */
    @ApiOperation(value = "删除行程")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        Long[] ids= {id};
        return toAjax(tripService.updateTripByIds(ids));
    }
}
